<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.regex.*" %>
<%@ page import="java.util.Vector" %>
<%@ page import="net.canadensys.dataportal.vascan.utils.Fulltext" %>
<%@ page import="org.apache.commons.collections.iterators.FilterIterator" %>
<%@ page import="org.apache.commons.collections.Predicate" %>
<%@ page import="org.json.*" %>
<%@ include file="_inc/functions.jsp" %>
<%  String TEMPLATE_PATH = "/search.ftl"; %>
<%@ include file="_inc/ftl/processStart.jsp" %>
<%
// Copyright (c) 2010 Canadensys
try{
    //select box
    String selected = "selected=\"selected\"";
    String all = "";
    String fsn = "";
    String vrn = "";

    //search 
    String term = "";
    Vector<HashMap<String,Object>> results = new Vector<HashMap<String,Object>>();
    JSONObject data = new JSONObject();
    JSONArray items;
    int totalResults = 0;
    boolean searchFullScientificName = true;
    boolean searchVernacularName = true;
    
    if(request.getParameter("q") != null){
        term = request.getParameter("q").trim();
        boolean displayFullResults = true;
        if(!term.equals("")){
	        data = (JSONObject) Fulltext.search(term,searchFullScientificName,searchVernacularName,0);
	        if(data != null && data.length() > 0){
	            items = (JSONArray) data.get("d");
	            totalResults = (Integer) data.get("t");
	            for(int i=0; i < items.length(); i++){
	                JSONObject item = items.getJSONObject(i);
	                HashMap<String,Object> result = new HashMap<String,Object>();
	                result.put("taxonId",item.getString("i"));
	                result.put("fullScientificName",item.getString("n"));
	                result.put("fullScientificNameHtml",item.getString("nh"));
	                result.put("fullScientificNameStatus",item.getString("s"));
	                result.put("frenchVernacularName",item.getString("f"));
	                result.put("frenchVernacularNameHtml",item.getString("fh"));
	                result.put("frenchVernacularNameStatus",item.getString("fs"));
	                result.put("englishVernacularName",item.getString("e"));
	                result.put("englishVernacularNameHtml",item.getString("eh"));
	                result.put("englishVernacularNameStatus",item.getString("es"));
	                results.add(result);
	            }
	        }
        }
    }
    
    HashMap<String,Object> search = new HashMap<String,Object>();
    search.put("term",term);
    search.put("total",totalResults);
    root.put("search",search);
    root.put("results",results);
    
    root.put("all",all);
    root.put("fsn",fsn);
    root.put("vrn",vrn);
}
catch(Exception e){   
	e.printStackTrace();
    out.write(e.toString());
}

%>
<%@ include file="_inc/ftl/processStop.jsp" %>